package com.hsxc.cdz.monitor.bean.second;

import java.io.UnsupportedEncodingException;

import com.hsxc.cdz.base.util.BytesUtil;
import com.hsxc.cdz.mgmt.GlobalManager;
import com.hsxc.cdz.monitor.FSM.CachePile;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.ReferenceCountUtil;

/**
 * 开始充电下行
 * @author llw
 * 
 */
public class ChargeSerMsg extends SecMessage {

	private String billNO; //交易流水号
	private CP56Time2a startTime; //预约开始时间
	private int reserveTime;  //预约时长  分钟
	private int fateVersion;  //电价费率版本号
	private int pileFate;     //占桩费率
	private int serviceFate;  //服务费率
	private Fate fate;        //电价费率

	public static final int length_billNO = 32;
	public static final int length_startTime = 7;
	public static final int length_reserveTime = 2;
	public static final int length_fateVersion = 4;
	public static final int length_pileFate = 4;
	public static final int length_serviceFate = 4;
	
	public static final int length = 72;
	public ChargeSerMsg(){
		setVersion(final_version);
		setFrame(frame_charge_ser);
	}
	
	public ChargeSerMsg(String pileNO,String billNO,CP56Time2a startTime,int reserveTime){
		this();
		setPileNO(pileNO);
		this.billNO=billNO;
		this.startTime=startTime;
		this.reserveTime = reserveTime;
		this.fateVersion = final_FateVersion;
		this.pileFate = final_PileFate;
		this.serviceFate = final_ServiceFate;
		this.fate = final_fate;
	}
	/************************************************************************/
	//重写父类方法
	@Override
	public byte[] getMessage() {
		ByteBuf bytes = Unpooled.buffer(length);
		byte[] result;
		try {
			bytes.writeBytes(BytesUtil.strToBinBytes(isStart, 1));
			bytes.writeBytes(BytesUtil.intToBytesDesc(length, length_length));
			bytes.writeBytes(BytesUtil.intToBytesSeq(getVersion(),length_version));
			bytes.writeBytes(BytesUtil.intToBytesDesc(getSerIndex(), length_seIndex));
			bytes.writeBytes(BytesUtil.intToBytesDesc(getPileIndex(), length_pileIndex));
			bytes.writeBytes(BytesUtil.strToBcdBytes(getPileNO(), length_pileNO));
		    bytes.writeBytes(BytesUtil.intToBytesSeq(getFrame(), length_frame));
		    bytes.writeBytes(billNO.getBytes("ascill"));
		    bytes.writeBytes(startTime.getCP());
		    bytes.writeBytes(BytesUtil.intToBytesDesc(reserveTime, length_reserveTime));
		    bytes.writeBytes(BytesUtil.intToBytesDesc(fateVersion, length_fateVersion));
		    bytes.writeBytes(BytesUtil.intToBytesDesc(pileFate, length_pileFate));
		    bytes.writeBytes(BytesUtil.intToBytesDesc(serviceFate, length_serviceFate));
		    bytes.writeBytes(fate.getFate());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		result = new byte[bytes.readableBytes()];
		bytes.readBytes(result);
		ReferenceCountUtil.release(bytes);
		return result;
	}

	@Override
	public boolean execute() {
		CachePile pile = GlobalManager.pileManager.getPile(getPileNO());
		pile.getChannel().writeAndFlush(this);
		return true;
	}

	@Override
	public void setMessage(byte[] data) {
		super.setMessage(data);
		try {
			int temp = SecMessage.length;
			this.billNO = new String(BytesUtil.getByte(temp,length_billNO, data), "ascill");
			temp = temp+length_billNO;
			this.startTime = new CP56Time2a(BytesUtil.getByte(temp, length_startTime, data));
			temp = temp+length_startTime;
			this.reserveTime = BytesUtil.bytesToIntDesc(BytesUtil.getByte(temp, length_reserveTime, data));
			temp = temp + length_reserveTime;
			this.fateVersion = BytesUtil.bytesToIntDesc(BytesUtil.getByte(temp, length_fateVersion, data));
			temp = temp+length_fateVersion;
			this.pileFate = BytesUtil.bytesToIntDesc(BytesUtil.getByte(temp, length_pileFate, data));
			temp = temp+length_pileFate;
			this.serviceFate = BytesUtil.bytesToIntDesc(BytesUtil.getByte(temp, length_serviceFate, data));
			temp = temp+length_serviceFate;
			this.fate = new Fate(BytesUtil.getByte(temp, Fate.length, data));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
	}

	/******************************************************************************/
	//get() and set()
	public String getBillNO() {
		return billNO;
	}

	public void setBillNO(String billNO) {
		this.billNO = billNO;
	}

	public CP56Time2a getStartTime() {
		return startTime;
	}

	public void setStartTime(CP56Time2a startTime) {
		this.startTime = startTime;
	}

	public int getReserveTime() {
		return reserveTime;
	}

	public void setReserveTime(int reserveTime) {
		this.reserveTime = reserveTime;
	}

	public int getFateVersion() {
		return fateVersion;
	}

	public void setFateVersion(int fateVersion) {
		this.fateVersion = fateVersion;
	}

	public int getPileFate() {
		return pileFate;
	}

	public void setPileFate(int pileFate) {
		this.pileFate = pileFate;
	}

	public int getServiceFate() {
		return serviceFate;
	}

	public void setServiceFate(int serviceFate) {
		this.serviceFate = serviceFate;
	}

	public Fate getFate() {
		return fate;
	}

	public void setFate(Fate fate) {
		this.fate = fate;
	}
}
